package com.hcy.common.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcy.common.CommonInfoBz;
import com.hcy.common.config.CarConfigDetails;
import com.hcy.common.config.RuntimeConfig;
import com.hcy.common.dto.CarLevelPriceConfig;
import com.hcy.common.dto.request.DirectionRequestDTO;
import com.hcy.common.dto.request.PushMsgRequestDTO;
import com.hcy.common.dto.response.CallCarConfigDTO;
import com.hcy.common.dto.response.MessageResponseDTO;
import com.hcy.common.entity.CallCarConfig;
import com.hcy.common.entity.GaodeGfids;
import com.hcy.common.entity.PushMessageRecord;
import com.hcy.common.service.impl.CallCarConfigServiceImpl;
import com.hcy.common.service.impl.GaodeGfidsServiceImpl;
import com.hcy.common.service.impl.PushMessageRecordServiceImpl;
import com.hcy.customer.CarLevelInfoResponse;
import com.hcy.driver.entity.CarLevel;
import com.hcy.driver.service.impl.CarLevelServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author huchenying
 * @Description
 * @Date 2022/3/12 10:37
 */
@Service
public class CommonInfoBzImpl implements CommonInfoBz {

    private static final Logger logger = LoggerFactory.getLogger(CommonInfoBzImpl.class);

    @Autowired
    private CarLevelServiceImpl carLevelService;

    @Autowired
    private GaodeGfidsServiceImpl gaodeGfidsService;

    @Autowired
    private RuntimeConfig runtimeConfig;

    @Autowired
    private CallCarConfigServiceImpl callCarConfigService;

    @Autowired
    private PushMessageRecordServiceImpl pushMessageRecordService;

    @Override
    public List<CarLevelPriceConfig> getCarLevelPriceConfig() {
        return carLevelService.getCarLevelPriceConfig();
    }

    /**
     * @Author huchenying
     * @Description 获取等级价格列表
     * @Date 2022/3/12 12:56
     **/
    @Override
    public List<CarLevelInfoResponse> getLevelPriceList(Integer distance, List<CarLevel> list) {
        List<CarLevelInfoResponse> responseArrayList = new ArrayList<>();
        for (CarLevel carLevel : list) {
            //在这通过配置计算价格
            CarConfigDetails carConfigDetails = runtimeConfig.getCarConfigDetailsMap().get(carLevel.getLabel());
            CarLevelInfoResponse response = new CarLevelInfoResponse();
            response.setId(carLevel.getId());
            response.setIcon(carLevel.getIcon());
            response.setLabel(carLevel.getLabel());
            response.setChecked(false);
            response.setDesc("描述");
            //通过 distance 计算价格 ，暂时简单计价
            double money = 1;
            if (distance <= 1000) {
                money = 5;
            } else {
                money = distance / 1000.0 * 5;
            }
            response.setMoney(money);
            responseArrayList.add(response);
        }
        return responseArrayList;
    }

    @Override
    public List<GaodeGfids> getGfids() {
        return gaodeGfidsService.list();
    }

    @Override
    public List<CallCarConfigDTO> getCallCarConfig() {
        List<CallCarConfigDTO> list = new ArrayList<>();
        List<CallCarConfig> res = callCarConfigService.list();
        if (CollectionUtils.isEmpty(res)) {
            throw new RuntimeException("叫车的配置callCarConfig为空");
        }
        res.sort((Comparator.comparingInt(CallCarConfig::getRound)));
        for (CallCarConfig config : res) {
            CallCarConfigDTO callCarConfigDTO = new CallCarConfigDTO();
            BeanUtils.copyProperties(config, callCarConfigDTO);
            list.add(callCarConfigDTO);
        }
        logger.info("获取叫车配置 list -> {}", ToStringBuilder.reflectionToString(list));
        return list;
    }

    @Override
    public void insertMessage(PushMsgRequestDTO requestDTO) {
        PushMessageRecord pushMessageRecord = new PushMessageRecord();
        BeanUtils.copyProperties(requestDTO, pushMessageRecord);
        boolean save = pushMessageRecordService.save(pushMessageRecord);
        if (!save) {
            throw new RuntimeException("消息添加失败！");
        }
    }

    @Override
    public List<MessageResponseDTO> queryMessage(PushMsgRequestDTO requestDTO) {

        LambdaQueryWrapper<PushMessageRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(requestDTO.getTypeId() != null, PushMessageRecord::getTypeId, requestDTO.getTypeId());
        queryWrapper.eq(requestDTO.getMaster() != null, PushMessageRecord::getMaster, requestDTO.getMaster());
        queryWrapper.eq(requestDTO.getIsVisited() != null, PushMessageRecord::getIsVisited, requestDTO.getIsVisited());
        queryWrapper.eq(StringUtils.isNotEmpty(requestDTO.getDriverName()), PushMessageRecord::getDriverName, requestDTO.getDriverName());
        queryWrapper.eq(StringUtils.isNotEmpty(requestDTO.getCustomerName()), PushMessageRecord::getCustomerName, requestDTO.getCustomerName());
        queryWrapper.orderBy(true,false,PushMessageRecord::getTime);
        Page<PushMessageRecord> page = new Page<>();
        page.setSize(requestDTO.getPageSize());
        page.setCurrent(requestDTO.getPageNo());

        Page<PushMessageRecord> pushMessageRecordPage = pushMessageRecordService.page(page, queryWrapper);
        List<MessageResponseDTO> responseDTOList = new ArrayList<>();
        for (PushMessageRecord pushMessageRecord : pushMessageRecordPage.getRecords()) {
            MessageResponseDTO messageResponseDTO = new MessageResponseDTO();
            BeanUtils.copyProperties(pushMessageRecord, messageResponseDTO);
            responseDTOList.add(messageResponseDTO);
        }
        return responseDTOList;
    }

}
